package clientSide;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.json.simple.JSONObject;

import common.Message;
import common.MessageType;
import common.TaskResult;

public class Login extends Shell {
	private Image logo;
	private final Text textUserName;
	private final Text textPassword;
	private Client client;

	public void Show() {
		open();
		layout();
		while (!isDisposed()) {
			if (!Display.getDefault().readAndDispatch())
				Display.getDefault().sleep();
			else {
				if (client.HasMessage()) {
					Message msg;
					try {
						msg = client.getMessage(true);
						if (msg.getType() == MessageType.Login
								&& msg.getTaskResult() == TaskResult.OK) {
							client.logined = true;
							break;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public Login(Display display, final Client client) {
		super(display, SWT.DIALOG_TRIM | SWT.MIN);
		setBackground(SWTResourceManager.getColor(51, 153, 255));
		setLocation(500, 100);
		this.client = client;

		final Canvas canvas = new Canvas(this, SWT.NONE);
		canvas.setBackground(SWTResourceManager.getColor(51, 153, 255));
		canvas.setBounds(56, 10, 153, 96);

		Composite LoginInfo = new Composite(this, SWT.NONE);
		LoginInfo.setBackground(SWTResourceManager.getColor(51, 153, 255));
		LoginInfo.setBounds(20, 128, 217, 386);

		Label label = new Label(LoginInfo, SWT.NONE);
		label.setText("UserName:");
		label.setBackground(SWTResourceManager.getColor(51, 153, 255));
		label.setBounds(10, 10, 197, 15);

		textUserName = new Text(LoginInfo, SWT.BORDER);
		textUserName.setText("Manh");
		textUserName.setSelection(6);
		textUserName.setBackground(SWTResourceManager.getColor(255, 255, 255));
		textUserName.setBounds(10, 31, 197, 21);

		Label label_1 = new Label(LoginInfo, SWT.NONE);
		label_1.setText("Password:");
		label_1.setBackground(SWTResourceManager.getColor(51, 153, 255));
		label_1.setBounds(10, 69, 55, 15);

		textPassword = new Text(LoginInfo, SWT.BORDER | SWT.PASSWORD);
		textPassword.setText("manh");
		textPassword.setBackground(SWTResourceManager.getColor(255, 255, 255));
		textPassword.setBounds(10, 90, 197, 21);

		Button checkBoxRem = new Button(LoginInfo, SWT.CHECK);
		checkBoxRem.setText("Remember Login Info");
		checkBoxRem.setSelection(true);
		checkBoxRem.setImage(null);
		checkBoxRem.setBackground(SWTResourceManager.getColor(51, 153, 255));
		checkBoxRem.setBounds(10, 135, 197, 16);

		Button checkBoxInvisible = new Button(LoginInfo, SWT.CHECK);
		checkBoxInvisible.setText("Invisible for everyone");
		checkBoxInvisible.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_LIST_SELECTION));
		checkBoxInvisible.setBackground(SWTResourceManager.getColor(51, 153,
				255));
		checkBoxInvisible.setBounds(10, 157, 197, 16);

		Button btnLogin = new Button(LoginInfo, SWT.NONE);
		btnLogin.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// test login

				Display.getDefault().asyncExec(new Runnable() {

					@SuppressWarnings("unchecked")
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try {
							client.setUserName(textUserName.getText());
							JSONObject obj = new JSONObject();
							obj.put("User", textUserName.getText());
							obj.put("Pass", textPassword.getText());
							obj.put("Port",client.getClientId() );							
							client.SendMessage(new Message(	 textUserName.getText(),
									"SERVER",
									MessageType.Login,obj.toJSONString() .getBytes()));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

			}
		});
		btnLogin.setBackground(SWTResourceManager.getColor(204, 255, 255));
		btnLogin.setText("Sign In");
		btnLogin.setBounds(68, 206, 75, 25);

		Link linkResetPass = new Link(LoginInfo, 0);
		linkResetPass.setText("<a>Forgot Password</a>");
		linkResetPass.setBackground(SWTResourceManager.getColor(51, 153, 255));
		linkResetPass.setBounds(49, 273, 108, 15);

		Link linkCreateAccount = new Link(LoginInfo, 0);
		linkCreateAccount.setText("<a>Register new Account</a>");
		linkCreateAccount.setBackground(SWTResourceManager.getColor(51, 153,
				255));
		linkCreateAccount.setBounds(49, 305, 128, 15);
		createContents();
		ImageData data = new ImageData("src//Image//yahoo.png");
		logo = new Image(canvas.getDisplay(), data.scaledTo(canvas.getSize().x,
				canvas.getSize().y));
		canvas.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent arg0) {
				// TODO Auto-generated method stub
				arg0.gc.drawImage(logo, 0, 0);
			}
		});
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Login");
		setSize(266, 553);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
